package com.hnust.interceptor;

import com.hnust.controller.LoginController;
import com.hnust.store.DataStore;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class RequestInterceptors implements RequestInterceptor {

    private static final Logger logger = LogManager.getLogger(LoginController.class);

    @Autowired
    private DataStore dataStore;

    /**
     *  每次请求时都会拦截到
     * @param requestTemplate 请求对象
     */
    @Override
    public void apply(RequestTemplate requestTemplate) {
        if(dataStore.getToken()!=null){
            if(requestTemplate.url().contains("updateInfo")||requestTemplate.url().contains("/upload/question")){
                logger.info("拦截到特殊url,不注入token");
            }else{
                logger.info("拦截到请求对象，注入 token 字段");
                requestTemplate.query("token",dataStore.getToken());
            }
        }
    }
}
